//
//  KYUserAccountTool.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/29.
//  Copyright © 2017年 ???. All rights reserved.
//

#define kToken @"taobao_token"
#define kPhone @"userPhone"
#define kTaobaoKey @"taobao_key"
#define kHeadImg @"headImg"
#define kUserName @"userName"
#define kUserSex @"userSex"
#define kUserBirth @"userBirth"
#define kUserIntro @"userIntro"


#import "KYUserAccountTool.h"

@implementation KYUserAccountTool

- (void)encodeWithCoder:(NSCoder *)aCoder{
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    //    self = [super init];
    //    if (self) {
//    self.name = [aDecoder decodeObjectForKey:@"name"];
//    self.age = [aDecoder decodeIntegerForKey:@"age"];
    //    }
    return self;
}

+(instancetype)shareUserData {
    return [[self alloc] init];
}

#pragma mark - 分配内存创建对象
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static KYUserAccountTool *data;
    // 为了线程安全
    // 三个线程同时调用这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [super allocWithZone:zone];
        // 从沙盒获取上次的用户登录信息
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        data.token = [defaults objectForKey:kToken];
        data.taobaoKey = [defaults objectForKey:kTaobaoKey];
        data.userPhone = [defaults objectForKey:kPhone];
        data.headImg = [defaults objectForKey:kHeadImg];
        data.userName = [defaults objectForKey:kUserName];
        data.userSex = [defaults objectForKey:kUserSex];
        data.userBirth = [defaults objectForKey:kUserBirth];
        data.userIntro = [defaults objectForKey:kUserIntro];
    });
    
    return data;
}

- (void)saveToSandBox{
    // 保存 icon name sex  birth pofession mark
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.token forKey:kToken];
    [defaults setObject:self.taobaoKey forKey:kTaobaoKey];
    [defaults setObject:self.userPhone forKey:kPhone];
    [defaults setObject:self.headImg forKey:kHeadImg];
    [defaults setObject:self.userName forKey:kUserName];
    [defaults setObject:self.userSex forKey:kUserSex];
    [defaults setObject:self.userBirth forKey:kUserBirth];
    [defaults setObject:self.userIntro forKey:kUserIntro];
    [defaults synchronize];
}

- (void)clearFromSandBox{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:kToken];
    [defaults removeObjectForKey:kTaobaoKey];
    [defaults removeObjectForKey:kPhone];
    [defaults synchronize];
    self.token = nil;
    self.taobaoKey = nil;
    self.userPhone = nil;
}
@end
